package logic

import (
	"context"
	"fmt"
	"muke-network/srv/model/mysql"

	"muke-network/srv/internal/svc"
	"muke-network/srv/muke"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *muke.UserLoginRequest) (*muke.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{
		Username: in.Username,
		Password: in.Password,
	}

	err := u.FindUser(in.Username)
	if err != nil {
		logx.Error("用户查询失败")
		return nil, fmt.Errorf("用户查询失败")
	}
	if u.ID == 0 {
		logx.Error("用户不存在")
		return nil, fmt.Errorf("用户不存在")
	}

	return &muke.UserLoginResponse{UserId: int64(u.ID)}, nil
}
